package com.senseauto.mediacodec_surface;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;


public class RecordActivity extends AppCompatActivity implements View.OnClickListener{
    String TAG = "RecordActivity";
    private Button btRecoder;
    MediaProjectionManager mediaProjectionManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera2);
        btRecoder = findViewById(R.id.btnRecord);
        btRecoder.setOnClickListener(this);
    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        if(requestCode ==1 && resultCode==RESULT_OK){
            Log.e(TAG, "保存intent和resultCode");
            MainApplication.getInstance().setResultIntent(data);
            MainApplication.getInstance().setResultCode(resultCode);
            startService(new Intent(getApplicationContext(), RecordService.class));
        }
     }

    @Override
    public void onClick(View view) {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    && checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED
            ) {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.RECORD_AUDIO}, 100);
            }else{
                mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
                MainApplication.getInstance().setMediaProjectionManager(mediaProjectionManager);
                Intent intent = mediaProjectionManager.createScreenCaptureIntent();
                startActivityForResult(intent, 1);
            }
        }else{
            mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
            MainApplication.getInstance().setMediaProjectionManager(mediaProjectionManager);
            Intent intent = mediaProjectionManager.createScreenCaptureIntent();
            startActivityForResult(intent, 1);
        }

    }

}